{% extends "base.html" %}
{% block content %}
<h1>(more) Real Form View</h1>
<p>This form makes some process: it renders back what you typed but
uncoverring the first char of your password</p>
<form action="" method="POST">
{{ form }}
<input type="submit">
</form>
<div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">obviews.views</span> <span class="kn">import</span> <span class="n">View</span><span class="p">,</span> <span class="n">FormView</span><span class="p">

<span class="c"># Register</span>
<span class="k">class</span> <span class="nc">RegisterForm</span><span class="p">(</span><span class="n">FormView</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/register_form.html&quot;</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;on_success&quot;</span><span class="p">:</span> <span class="s">&quot;/form/registered/&quot;</span><span class="p">}</span>
    <span class="n">form_class</span> <span class="o">=</span> <span class="n">TestForm</span>

    <span class="k">def</span> <span class="nf">process_form</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">form</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="n">username</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">]</span>
        <span class="n">password</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="p">{</span><span class="s">&quot;username&quot;</span><span class="p">:</span> <span class="n">username</span><span class="p">,</span>
                <span class="s">&quot;password&quot;</span><span class="p">:</span> <span class="n">password</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="s">&#39;*&#39;</span><span class="o">*</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">password</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">)}</span>

    <span class="k">def</span> <span class="nf">on_success</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">):</span>
        <span class="n">response</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">RegisterForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">on_success</span><span class="p">(</span>
            <span class="n">result</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">variables</span><span class="p">)</span>
        <span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s">&quot;username&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s">&#39;username&#39;</span><span class="p">])</span>
        <span class="n">response</span><span class="o">.</span><span class="n">set_cookie</span><span class="p">(</span><span class="s">&quot;password&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="s">&#39;password&#39;</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">response</span>
        
<span class="k">class</span> <span class="nc">RegisterView</span><span class="p">(</span><span class="n">View</span><span class="p">):</span>
    <span class="n">template_name</span> <span class="o">=</span> <span class="s">&quot;forms/registered.html&quot;</span>

    <span class="k">def</span> <span class="nf">build_params</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">COOKIES</span>
        
<span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span>                
    <span class="p">(</span><span class="s">&#39;^form/register/$&#39;</span><span class="p">,</span> <span class="n">RegisterForm</span><span class="p">()),</span>
    <span class="p">(</span><span class="s">&#39;^form/registered/$&#39;</span><span class="p">,</span> <span class="n">RegisterView</span><span class="p">()),</span>
 <span class="p">)</span>

</pre></div>
<p>
  Now you have really all what you need to build your forms. The
  <strong>next example</strong> only recalls that FormViews are Views,
  and you can consequently use all included batteries, ie <a
  href="/form/cut/">inheriting and stopping process</a>
</p>
{% endblock %}
